import { isFunction, isObject } from "../types"
import { createVNode } from "../core/vnode"

export const createElement = (type, attributes, ...children) => {
  let props = {}
  let key = null
  let ref = null
  for (let propName in attributes) {
    if (propName === 'key') {
      key = attributes[propName]
    } else if (propName === 'ref') {
      ref = attributes[propName]
    } else {
      props[propName] = attributes[propName]
    }
  }

  props.children = children
  
  if (isFunction(type) && isObject(type.defaultProps)) {
    for (let propName in type.defaultProps) {
      if (!props[propName]) {
        props[propName] = type.defaultProps[propName]
      }
    }
  }

  return createVNode(type, key, ref, props)
}